// API调用示例
// 在Vue组件中使用

// 1. 获取医院缩写列表
const getHspAbbrList = async () => {
  try {
    const response = await axios.post('/api/rule_apply/list/hsp_abbr')
    if (response.data.mark === 'OK') {
      return response.data.data // 返回医院缩写数组
    }
  } catch (error) {
    console.error('获取医院列表失败:', error)
    return []
  }
}

// 2. 根据ruleId和hspAbbr查询规则应用数据
const getRuleApplyData = async (ruleId, hspAbbr) => {
  try {
    const response = await axios.post('/api/rule_apply/query/by_rule_id', {
      ruleId: ruleId,
      hspAbbr: hspAbbr
    })
    
    if (response.data.mark === 'OK') {
      return response.data.data // 返回applyData数组
    } else {
      throw new Error(response.data.msg || '查询失败')
    }
  } catch (error) {
    console.error('查询规则应用数据失败:', error)
    throw error
  }
}

// 3. 在Vue组件中的完整使用示例
export default {
  setup() {
    const hspAbbrList = ref([])
    const applyData = ref([])
    const loading = ref(false)
    const selectedRuleId = ref('A45')
    const selectedHspAbbr = ref('妇产')
    
    // 初始化获取医院列表
    const initHspAbbrList = async () => {
      hspAbbrList.value = await getHspAbbrList()
    }
    
    // 查询规则应用数据
    const queryRuleApply = async () => {
      if (!selectedRuleId.value || !selectedHspAbbr.value) {
        ElMessage.warning('请选择规则ID和医院')
        return
      }
      
      loading.value = true
      try {
        applyData.value = await getRuleApplyData(
          selectedRuleId.value, 
          selectedHspAbbr.value
        )
        ElMessage.success('查询成功')
      } catch (error) {
        ElMessage.error(error.message || '查询失败')
        applyData.value = []
      } finally {
        loading.value = false
      }
    }
    
    // 页面加载时初始化
    onMounted(() => {
      initHspAbbrList()
    })
    
    return {
      hspAbbrList,
      applyData,
      loading,
      selectedRuleId,
      selectedHspAbbr,
      queryRuleApply
    }
  }
}

// 4. 模板使用示例
/*
<template>
  <div class="rule-apply-query">
    <el-form :inline="true">
      <el-form-item label="规则ID">
        <el-input v-model="selectedRuleId" placeholder="请输入规则ID" />
      </el-form-item>
      
      <el-form-item label="医院">
        <el-select v-model="selectedHspAbbr" placeholder="请选择医院">
          <el-option 
            v-for="hsp in hspAbbrList" 
            :key="hsp" 
            :label="hsp" 
            :value="hsp" 
          />
        </el-select>
      </el-form-item>
      
      <el-form-item>
        <el-button 
          type="primary" 
          @click="queryRuleApply" 
          :loading="loading"
        >
          查询
        </el-button>
      </el-form-item>
    </el-form>
    
    <!-- 使用ruleApply组件展示数据 -->
    <RuleApply :apply-data="applyData" />
  </div>
</template>
*/